namespace Billing2
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Security.Application;

    public class ucFinder : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnReset;
        protected Button btnSearch;
        protected DropDownList ddlFilterCondition;
        protected DropDownList ddlSearch;
        private DataRow dr;
        protected GridView GridViewSubscriber;
        protected Panel Panel1;
        protected TextBox txtSearch;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel1;
        protected HiddenField xOrgID;
        protected HiddenField xDialogID;
        protected HiddenField xParentID;
        protected HiddenField xPrimaryKey;
        protected HiddenField xQueryType;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void PrepareData()
        {
            this.GridViewSubscriber.Visible = true;
            this.ActiveGridView = this.GridViewSubscriber;
            this.ActiveGridView.PageIndex = 0;
            this.FilterString = "";
            this.BindGrid();
        }

        public DataTable GetData()
        {
            SqlCommand cm = new SqlCommand
            {
                CommandType = CommandType.StoredProcedure
            };
            cm.CommandText = "Sp_Subscriber_SelectAllByOrg";
            cm.Parameters.Add(new SqlParameter("@OrgID", this.OrgID));
            DataTable dt = new DBManager().executeCommandDataTable(cm);

            if (this.FilterString == "")
                return dt;
            else
            {
                DataView dv = new DataView(dt);
                dv.RowFilter = this.FilterString;
                return dv.ToTable();
            }
        }

        public void BindGrid()
        {
            DataTable dt = GetData();
            this.ActiveGridView.DataSource = dt;
            this.ActiveGridView.DataBind();
            if (dt.Rows.Count == 0)
            {
                this.ucHighlight1.Message = "No records found.";
            }
            else
            {
                this.ActiveGridView.PageIndex = 0;
                this.ucHighlight1.CloseAll();
            }
        }

        private string FilterString
        {
            get
            {
                return (string)this.ViewState["FilterString"];
            }
            set
            {
                this.ViewState["FilterString"] = value;
            }
        }

        public DataRow DataRowRecord
        {
            get
            {
                return this.dr;
            }
            set
            {
                this.dr = value;
            }
        }

        private GridView ActiveGridView
        {
            get
            {
                return (GridView)this.FindControl(Convert.ToString(this.ViewState["GridViewID"]));
            }
            set
            {
                this.ViewState["GridViewID"] = value.ID;
                int old_index = this.ddlSearch.SelectedIndex;
                this.ddlSearch.Items.Clear();
                for (int i = 0; i < value.Columns.Count; i++)
                {
                    this.ddlSearch.Items.Add(new ListItem(value.Columns[i].HeaderText, value.Columns[i].SortExpression));
                }
                if (old_index >= 0)
                    this.ddlSearch.SelectedIndex = old_index;
            }
        }
        
        protected void ActiveGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.ActiveGridView.PageIndex = e.NewPageIndex;
            this.ActiveGridView.DataSource = GetData();
            this.ActiveGridView.DataBind(); 
        }

        protected void ActiveGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString().ToLower() == "editdata")
            {
                this.PrimaryKey = Convert.ToInt64(e.CommandArgument);

                ClsSubscriber cls = new ClsSubscriber();
                cls.ID = this.PrimaryKey;
                DataTable dt = cls.Select();
                if (dt.Rows.Count > 0)
                {
                    this.DataRowRecord = dt.Rows[0];
                }

                ClsUtilityHelper.CloseJQueryDialog(this.DialogName, this);
                this.ucHighlight1.CloseAll();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            this.ucHighlight1.CloseAll();
            ClsUtilityHelper.ResetFormControlValues(this);
            this.FilterString = "";
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.ucHighlight1.CloseAll();
            if (this.ddlSearch.SelectedIndex < 0)
            {
                this.ucHighlight1.ErrorMessage = "Please Select Search Criteria";
            }
            else if (this.txtSearch.Text == "")
            {
                this.ucHighlight1.ErrorMessage = "Search Text required";
            }
            else
            {
                string filterStr = "";
                string key = this.ddlSearch.SelectedValue.Trim();
                string value = this.txtSearch.Text.ToString();
                if (key != "" && !Common.ValidFieldName(key))
                {
                    EPPLog.Logger.Debug("invalid char in seach string: '" + key + "'");
                    return;
                }
                if (value != "" && !Common.ValidSeachValue(value))
                {
                    EPPLog.Logger.Debug("invalid char in seach string: '" + value + "'");
                    return;
                }

                if (this.ddlFilterCondition.SelectedValue.ToLower().Equals("equals to"))
                {
                    filterStr = "Convert (" + key + ", 'System.String') = '" + value + "'";
                }
                else
                {
                    filterStr = "Convert (" + key + ", 'System.String') Like '%" + value + "%'";
                }
                this.FilterString = filterStr;
                BindGrid();
            }
        }


        
        public long OrgID
        {
            get
            {
                return Convert.ToInt64(this.xOrgID.Value);
            }
            set
            {
                this.xOrgID.Value = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToString(value));
            }
        }



        public string DialogName
        {
            get
            {
                return this.xDialogID.Value;
            }
            set
            {
                this.xDialogID.Value = value;
            }
        }

        public string ParentID
        {
            get
            {
                return this.xParentID.Value;
            }
            set
            {
                this.xParentID.Value = Microsoft.Security.Application.Encoder.HtmlEncode(value);
            }
        }

        public long PrimaryKey
        {
            get
            {
                return Convert.ToInt64(this.xPrimaryKey.Value);
            }
            set
            {
                this.xPrimaryKey.Value = Microsoft.Security.Application.Encoder.HtmlEncode(Convert.ToString(value));
            }
        }
    }
}
